﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using JsonServer.FYPDataSetTableAdapters;

namespace JsonServer
{
    public partial class Month : System.Web.UI.Page
    {
        Double[] electricFee = { 0.778, 0.893, 1.034, 1.319, 1.529, 1.616 };
        Double[] electricFeeRange = { 400, 600, 800, 800, 800, 0 };
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        int datacount = 0;
        protected void ButtonCalculateMonthPower_Click(object sender, EventArgs e)
        {
            string year = this.TextBoxOfMonthPageYear.Text;
            string month = this.TextBoxOfMonthPageMonth.Text;
            double totalOneMonthPower = Convert.ToDouble(GetOneMonthPower(year, month)) / 3600;
            double carbonEmissions = totalOneMonthPower * 0.785;
            this.LabelCarbonEmissions.Text = "Carbon emissions: " + carbonEmissions.ToString("#.###") + "kg";
            string formatedTotalPower = totalOneMonthPower.ToString("#.###");
            this.LabelMonthPower.Text = "Total one month power is " + formatedTotalPower + "kWh.";
            double efee = 0.0;
            totalOneMonthPower *= 1000;
            for (int i = 0; i < 6; i++)
            {
                if (totalOneMonthPower > electricFeeRange[i])
                {
                    efee = electricFee[i] * electricFeeRange[i];
                    totalOneMonthPower -= electricFeeRange[i];
                }
                else
                {
                    efee = electricFee[i] * totalOneMonthPower;
                    break;
                }
            }
            this.LabelElectricFee.Text = "Electric Fee (CLP) : $" + efee.ToString("#.##");

        }

        public string GetOneMonthPower(string year, string month)
        {
            // lookup person with the requested id 
            FYPPowerTableTableAdapter adap = new FYPPowerTableTableAdapter();
            var powerTable = adap.GetDataByMonth(year, month);
            double MonthPower = 0;

            foreach (var item in powerTable)
            {
                double eachPower = Convert.ToDouble(item.Power);
                MonthPower += eachPower;
                datacount++;
            }
            string sMonthPower = MonthPower.ToString("#.###0");
            return sMonthPower;
        }
    }
}